package com.example.controller;
import com.example.entity.Comment;
import com.example.entity.Goods;
import com.example.entity.History;
import com.example.entity.User;
import com.example.service.*;
import org.bouncycastle.crypto.Commitment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.awt.print.Book;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserService userService;
    @Autowired
    private HistoryService historyService;
    @RequestMapping("/detail")
    public String list(Model model, int gid, String author, HttpSession session){
        User user = (User) session.getAttribute("user");
        // 1. 准备基础数据
        String historytime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        Goods goodsDetail=goodsService.getGoodsById(gid);
        System.out.println(gid);
        User teacher=teacherService.getTeacherByAuthor(goodsDetail.getGid());
        System.out.println(teacher);
        System.out.println(teacher.getUid());
        History history=new History();
        history.setUid(user.getUid());
        history.setGid(gid);
        history.setGname(goodsDetail.getGname());
        history.setAuthor(goodsDetail.getAuthor());
        history.setImg(goodsDetail.getImg());
        history.setHistorytime(historytime);
        historyService.add(history);
        model.addAttribute("teacher",teacher);
        model.addAttribute("goodsDetail",goodsDetail);
        List<Comment> comments = commentService.getCommentsByGoodsId(gid);
        model.addAttribute("comments", comments);
        model.addAttribute("gid", gid);


        return "student/detail";
    }
    @GetMapping("/pay")
    public String payPage(@RequestParam int gid,
                          @RequestParam String name,
                          @RequestParam BigDecimal price,
                          Model model) {
        model.addAttribute("gid", gid);
        model.addAttribute("name", name);
        model.addAttribute("price", price);
        return "student/pay"; // 对应views/student/pay.jsp
    }
    //修改个人信息
    @GetMapping("/update/{uid}")
    public String toFindId(@PathVariable("uid") Integer uid, Model model) {
        User user = userService.getUserById(uid);
        model.addAttribute("user", user);
        return "student/update";
    }

    @PostMapping("/update")
    public ModelAndView update(User user)  {
        ModelAndView mv=new ModelAndView();
        int page = 1;
        int pageNum = 6;
        List<Goods> goods = this.goodsService.findGoodsByPage(page, pageNum);
        int totalPage = this.goodsService.totalPage(pageNum);
        mv.addObject("goods", goods);
        mv.addObject("currentPage", page);
        mv.addObject("totalPage", totalPage);
        mv.addObject("user", user);
       mv.setViewName("student/home");
       return mv;
    }
}
